package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;


@Controller
@RequestMapping("/bfhkcontrller")
public class bfhkcontrller {

	private static final Logger logger = Logger.getLogger(bfhkcontrller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response,HttpSession session , String limit,String DKZH) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		BODY b=new BODY(); 
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String QR =df.format(new Date()).toString();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ193");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setZJHM(sessionIn.getZJHM());
		b.setGRZH(sessionIn.getGRZH());
		b.setDKZH(DKZH);
		b.setRQ(QR);
		reqMap.put("BODY",b);
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+retMap);
		if(retMap.get("RETURNCODE").equals("100")){
			Map resMap=JSONUtil.toMap(JSONUtil.ToJsongString2(retMap.get("body")));
			if(sessionIn.getSJHM()!=null && sessionIn.getSJHM()!=""){
				resMap.put("SJHM", sessionIn.getSJHM());	
			}
			if(sessionIn.getDWZH().toString()!=null && sessionIn.getDWZH().toString()!=""){
				resMap.put("DWZH", sessionIn.getDWZH().toString());	
			}
			if(sessionIn.getGRZH().toString()!=null && sessionIn.getGRZH().toString()!=""){
				resMap.put("GRZH", sessionIn.getGRZH().toString());	
			}
			if(sessionIn.getDWMC().toString()!=null && sessionIn.getDWMC().toString()!=""){
				resMap.put("DWMC", sessionIn.getDWMC().toString());	
			}
			if(retMap.get("RETURNCODE").equals("100")){
				/*	List resList = JSONUtil.toList(resMap.get("body").toString());*/
					//map.put("count", resList.size());
					map.put("code", 0);
					map.put("msg", "1");
					map.put("data",resMap);
					list.add(retMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
			}else{
					map.put("code", 1);
					map.put("msg", "1");
					map.put("CW", retMap.get("RETURNDESC").toString());
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
			}
		}else{
			map.put("code", 1);
			map.put("msg", "1");
			map.put("CW", retMap.get("RETURNDESC").toString());
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		return null;
	}
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(
			HttpServletRequest request, HttpServletResponse response,HttpSession session,
			String XINGMING,String LSH,String ZJHM,String YHFX,String HJ,String DKZH,
			String WTYH,String WYJ,String YHBJ,String SJHM,String DWZH,String ZHYE,String JCYH,String DXYZM,
			String GRZH,String GJJHDE,String XJHDE,String YHLX,String GSDQDM,String DWMC) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Date d=new Date();
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
		 String RQ= sdf.format(d);  
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		Map reqMap = new HashMap();
		reqMap.put("AUTHORITY", "YBGJJ194");
			b.setCZY("网厅");
			if(sessionIn.getXINGMING() !=null && !"".equals(sessionIn.getXINGMING())){
				b.setXINGMING(sessionIn.getXINGMING());	
			}else{
				b.setXINGMING("");
			}
			if(LSH !=null && !"".equals(LSH)){
				b.setLSH(LSH);	
			}else{
				b.setLSH("");	
			}
			if(ZJHM !=null && !"".equals(ZJHM)){
				b.setZJHM(ZJHM);	
			}else{
				b.setZJHM("");	
			}
			if(RQ !=null && !"".equals(RQ)){
				b.setRQ(RQ);	
			}else{
				b.setRQ("");	
			}
			if(YHFX !=null && !"".equals(YHFX)){
				b.setYHFX(YHFX);	
			}else{
				b.setYHFX("");	
			}
			if(HJ !=null && !"".equals(HJ)){
				b.setHJ(HJ);	
			}else{
				b.setHJ("");	
			}
			if(DKZH !=null && !"".equals(DKZH)){
				b.setDKZH(DKZH);	
			}else{
				b.setDKZH("");	
			}
			if(WTYH !=null && !"".equals(WTYH)){
				b.setWTYH(WTYH);	
			}else{
				b.setWTYH("");		
			}
			if(WYJ !=null && !"".equals(WYJ)){
				b.setWYJ(WYJ);	
			}else{
				b.setWYJ("");
			}
			if(YHBJ !=null && !"".equals(YHBJ)){
				b.setYHBJ(YHBJ);	
			}else{
				b.setYHBJ("");
			}
			if(YHLX !=null && !"".equals(YHLX)){
				b.setYHLX(YHLX);	
			}else{
				b.setYHLX("");	
			}
			if(SJHM !=null && !"".equals(SJHM)){
				b.setSJHM(SJHM);	
			}else{
				b.setSJHM("");	
			}
			if(DWZH !=null && !"".equals(DWZH)){
				b.setDWZH(DWZH);	
			}else{
				b.setDWZH("");	
			}
			if(ZHYE !=null && !"".equals(ZHYE)){
				b.setZHYE(ZHYE);	
			}else{
				b.setZHYE("");		
			}
			if(JCYH !=null && !"".equals(JCYH)){
				b.setJCYH(JCYH);	
			}else{
				b.setJCYH("");		
			}
			if(GRZH !=null && !"".equals(GRZH)){
				b.setGRZH(GRZH);	
			}else{
				b.setGRZH("");		
			}
			if(GJJHDE !=null && !"".equals(GJJHDE)){
				b.setGJJHDE(GJJHDE);	
			}else{
				b.setGJJHDE("0");	
			}
			if(DWMC !=null && !"".equals(DWMC)){
				b.setDWMC(DWMC);	
			}else{
				b.setDWMC("");	
			}
			if(XJHDE !=null && !"".equals(XJHDE)){
				b.setXJHDE(XJHDE);	
			}else{
				b.setXJHDE("0");		
			}
			if(GSDQDM !=null && !"".equals(GSDQDM)){
				b.setGSDQDM(GSDQDM);	
			}else{
				b.setGSDQDM("");			
			}
			if(DXYZM !=null && !"".equals(DXYZM)){
				b.setDXYZM(DXYZM);	
			}else{
				b.setDXYZM("");			
			}
		
		
		reqMap.put("OpenKey",sessionIn.getKEY());
		reqMap.put("BODY",b);
		String  result = opk.getOpenKey(reqMap, session);
		logger.info("YBGJJ166接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				map.put("code", 0);
				map.put("msg", "1");
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
	
		return null;
	}
}
